package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.CheckBox;
import com.cy.pj.sys.dao.SysRoleDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.pojo.SysRole;
import com.cy.pj.sys.service.SysRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleDao sysRoleDao;
    @Resource
    private SysRoleMenuDao sysRoleMenuDao;

    @Override
    public List<SysRole> findRoles(SysRole entity) {
        return sysRoleDao.selectRoles(entity);
    }

    @Override
    public int saveRole(SysRole entity) {
        int i = sysRoleDao.insertRole(entity);
        sysRoleMenuDao.insertRoleMenus(entity.getId(),entity.getMenuIds());
        return i;
    }

    @Override
    public SysRole findById(Integer id) {
        return sysRoleDao.selectById(id);
    }

    @Override
    public int updateRole(SysRole entity) {
        int i = sysRoleDao.updateRole(entity);

        if (i==0) throw new ServiceException("记录可能不存在");
        sysRoleMenuDao.deleteByRoleId(entity.getId());
        sysRoleMenuDao.insertRoleMenus(entity.getId(),entity.getMenuIds());
        return i;
    }

    @Override
    public List<CheckBox> findCheckRoles() {
        return sysRoleDao.selectCheckRoles();
    }

}
